473,421 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

How to get the absolute position of an element

Sorry if I'm posting a similar query again but I think this explanation of
what I require is better.

I have a page which is very long and as I scroll down the page there are
various links, what I'd like to be able to do is when I click on a link
return the absolute position within the page of that link.

I've looked at event.clientY but this just gives me the offset from the top
of the current page, suppose I've scrolled down by 3 pages ?

It's driving me nuts! hope someone can help.

TIA.

Chris
Jul 20 '05 #1
5 128411
function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

Will return the Y position of the object on the page.

Fred Basset
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #2
You are a star sir!

"Fred Basset" <fr*********@whosyourdaddy.com> wrote in message
news:3f***********************@news.frii.net...
function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

Will return the Y position of the object on the page.

Fred Basset
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jul 20 '05 #3
> function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}

Will return the Y position of the object on the page.

Umm hang on a minute .... it's not working!

Here is what I have.

var iReturnValue = 0;
elementid=document.getElementById(layerID)
while( elementid != null ){
iReturnValue += elementid.offsetTop;
elementid = elementid.offsetParent;
}
alert("link:"+iReturnValue);

Each time I scroll down the page and click on a link I get the value 10. Why
is this ? Could it be because I have the links in a table cell and that is
the parent ? Not too sure as I'm not that hot on JavaScript, can you help
please ?

TIA

Chris
Jul 20 '05 #4
I'm really sorry, but I don't know why it's not working for you. I'm
limited in the browsers I have installed at the moment, as I'm behind a
firewall, so I only have IE6 and NN7 installed, but it seems to work
fine in both of them.

Couple of things you can check.

Firstly, make sure you are passing in the id of the correct element.

Secondly, make sure that no other element on that page has the same
id/name.

Sorry I can't help further, but the code (my version AND yours) appears
to work without problems. Good luck finding the solution.

(Bear in mind that if you're using a browser other than the one above
then there may be cross-browser issues that I can't test)

Fred Basset
fr*********@whosyourdaddy.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Hi

I found the problem I had put the link inside a DIV to test something else
out and forgot to remove it - Doh!

Worked fine as soon as I removed the DIV.

Chris
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stephan Koser | last post by:
Hi, how can I get the absolute position of a relative element? We dynamically create a page with multiple segments which are relatively ordered among each other. In these segments we have...
1
by: Luqman | last post by:
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Best Regards, Luqman
4
by: Samuel | last post by:
Hi, Is it possible to get absolute position of element with javascript? e.g. I have textbox somewhere in table. Textbox relative to table cell position is 0,0. How can I get Textbox absolute...
1
by: SolFrankRosen | last post by:
I'm trying to make a button on my form be in a fixed position, I used: <asp:Button ID="butCancel" style="position:absolute"; runat="server" Text="Cancel" /> but I get an error that this "is...
0
by: Microsoft Newsserver | last post by:
Im using IE 6/7 / ASP.NET / Javascript. I need to determine the absolute position of elements on my page client side. In summart, I need to know if an event triggered mouse position appears...
30
by: Bassem | last post by:
Hi, I've two div.s. One contains four images, when click on one, a JS function create an image corresponding to it and append it to the second div. I'm attempting to make each new image is to be...
7
by: chemlight | last post by:
I am having an issue with HTML elements not printing when positioned absolutely when they extend beyond the first page. I am working on some foreign tax refund forms. The forms are cut into multiple...
0
by: shu fenghua | last post by:
The problem is when i want to put an element("a" in my code) into an absolute positioned element and i also want this element("a") has a fixed height, then ie6 will render this element("a") as a...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.